package com.shuwen.gcdj.service.statistic;

import com.shuwen.gcdj.bean.conditionMonitoring.request.MechanismVibrationReq;
import com.shuwen.gcdj.bean.conditionMonitoring.request.ShockSensorObjectQueryReq;
import com.shuwen.gcdj.bean.conditionMonitoring.response.StatisticsMechanismVibrationResp;
import com.shuwen.gcdj.bean.sysDictionary.request.SysDictionaryReq;
import com.shuwen.gcdj.bean.sysDictionary.response.SysDictionaryResp;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.common.constant.Constants;
import com.shuwen.gcdj.entity.statistics.StatisticsMechanismvibration;
import com.shuwen.gcdj.mapper.mysql.Statistic.StatisticsMechanismvibrationMapper;
import com.shuwen.gcdj.mapper.mysql.sysDictionary.SysDictionaryMapper;
import com.shuwen.gcdj.service.BaseService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Yangx
 */
@Service
@AllArgsConstructor
@Slf4j
public class StatisticsMechanismVibrationService extends BaseService<StatisticsMechanismvibrationMapper, StatisticsMechanismvibration> {
    private final StatisticsMechanismvibrationMapper statisticsMechanismvibrationMapper;
    private final SysDictionaryMapper sysDictionaryMapper;

    public CommonResult<List<StatisticsMechanismVibrationResp>> getShockSensorMechanismVibration(ShockSensorObjectQueryReq req) {
        List<StatisticsMechanismVibrationResp> statisticsMechanismVibrationRespList = new ArrayList<>();
        MechanismVibrationReq mechanismVibrationReq = new MechanismVibrationReq();
        mechanismVibrationReq.setEquipmentCode(req.getEquipmentCode());
        //获取当日最新数据
        LocalDate date = LocalDate.now();
        mechanismVibrationReq.setPosition(Integer.parseInt(req.getMonitoringObject()));
        mechanismVibrationReq.setYear(date.getYear());
        mechanismVibrationReq.setMonth(date.getMonthValue());
        mechanismVibrationReq.setDay(date.getDayOfMonth());

        List<StatisticsMechanismVibrationResp> mechanismvibrationList = statisticsMechanismvibrationMapper.getListByQuery(mechanismVibrationReq);

        if (CollectionUtils.isEmpty(mechanismvibrationList)) {
            return CommonResult.success(statisticsMechanismVibrationRespList);
        }
        List<String> sources = mechanismvibrationList.stream().map(StatisticsMechanismVibrationResp::getVibrationSource).distinct().collect(Collectors.toList());
        sources.forEach(source -> {

            statisticsMechanismVibrationRespList.add(mechanismvibrationList.stream().filter(mechanismvibration -> mechanismvibration.getVibrationSource().equals(source)).findFirst().orElse(new StatisticsMechanismVibrationResp()));
        });
        return CommonResult.success(statisticsMechanismVibrationRespList);
    }

}
